﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace prova
{
    public partial class Form1 : Form
    {
        SqlCeConnection con = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            inicialitzarRegistres();
        }

        /*
         * ((en favoritos mozilla))
        public void actualitzar(string consulta)
        {
            String datasource = @"Data Source=C:\Users\paco\cole\2 DAI\DRSPEG\UD2\BBDD\prova\prova\Alumnes.sdf";
            dataAdapter = new SqlDataAdapter(consulta, datasource);
            SqlCommandBuilder comando = new SqlCommandBuilder(dataAdapter);
            DataTable tabla = new DataTable();
            dataAdapter.Fill(tabla);
            bindingSouce1.DataSource = tabla;
            
        }
         */

        public void inicialitzarRegistres()
        {
            con = BaseDades.connectar();
            con.Open();

            SqlCeCommand com = new SqlCeCommand("SELECT * FROM Alumnes", con);
            SqlCeDataReader r = com.ExecuteReader();

            //afegir columnes dins el control
            //dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn());

            while (r.Read())
            {
                //creo una fila
                Object[] arr = new Object[3];
                arr[0] = r["NMatricula"];
                arr[1] = r["Nom"];
                arr[2] = r["Cognom"];

                //afegeixo la fila al GridView;
                dataGridView1.Rows.Add(arr);
            }
            
        }

        private void mostrarFormulariCrear()
        {
            FrmCrearUsuari formulariCrear = new FrmCrearUsuari();
            formulariCrear.Show();
            formulariCrear.FormClosed += new FormClosedEventHandler(formulariCrear_FormClosed);
        }

        void formulariCrear_FormClosed(object sender, FormClosedEventArgs e)
        {
            netejarGrid();
            inicialitzarRegistres();
        }

        private void btnSortir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void usuariToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mostrarUsuari();
        }

        private void btnVeureUsuari_Click_1(object sender, EventArgs e)
        {
            mostrarUsuari();
        }

        private void btnCrearUsuari_Click(object sender, EventArgs e)
        {
            mostrarFormulariCrear();
        }

        private void usuariToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            mostrarFormulariCrear();
        }

        private void mostrarUsuari()
        {
            int id = 0;

            //seleccionar el Nmatricula de l'alumne.
            DataGridViewSelectedRowCollection fSeleccionades = dataGridView1.SelectedRows;
            if (fSeleccionades.Count > 0)
            {
                id = (int)fSeleccionades[0].Cells[0].Value;
            }

            //carregar un alumnes de la bd.
            AlumneConversor acv = new AlumneConversor();
            acv.Nmatricula = id;
            acv.load();

            //pasar l'alumne a formulari.
            Form2 formulariVeureDades = new Form2();
            formulariVeureDades.setAlumne(acv);
            formulariVeureDades.Show();
            formulariVeureDades.FormClosed += 
                new FormClosedEventHandler(formulariVeureDades_FormClosed);

            /*
            DataGridViewSelectedRowCollection fSeleccionades = dataGridView1.SelectedRows;
            if (fSeleccionades.Count > 0)
            {
                int id = (int)fSeleccionades[0].Cells[0].Value;

                //Carreguem l'objecte alumne.
                Alumne a = new Alumne();
                a.Nmatricula = Convert.ToInt16(fSeleccionades[0].Cells[0].Value);
                a.Nom = (string)fSeleccionades[0].Cells[1].Value;
                a.Cognom = (string)fSeleccionades[0].Cells[2].Value;

                Form2 frmDadesAlumne = new Form2();
                frmDadesAlumne.setAlumne(a);
                frmDadesAlumne.Show();

                //exemple encapsulament
                //a.setCognom("Terradellas");
                //es igual que:
                //a.cognom = "terradellas";
                //per fer un get
                //console.WriteLine(a.cognom);
                
            }
             */
        }

        void formulariVeureDades_FormClosed(object sender, FormClosedEventArgs e)
        {
            netejarGrid();
            inicialitzarRegistres();
        }
        
        public void netejarGrid()
        {
            //limpiamos los datos de la data grid
            //optenemos cuantas filas tiene y las eleminamos 1 a 1           
            for (int i = dataGridView1.Rows.Count-1; i >= 1; i--)
            {
                dataGridView1.Rows.RemoveAt(0);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            int id = 0;

            //seleccionar el Nmatricula de l'alumne.
            DataGridViewSelectedRowCollection fSeleccionades = dataGridView1.SelectedRows;
            if (fSeleccionades.Count > 0)
            {
                id = (int)fSeleccionades[0].Cells[0].Value;
            }
        }
    }
}

//agafar totes les dades d'un alumne de la base de dades, pasant una id d'alumne i 
//que et retorni tot un alumne